package com.study.clothes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jhlabs.image.ImageUtils;
import com.study.clothes.pojo.Goods;
import com.study.clothes.pojo.RespBean;
import com.study.clothes.pojo.User;
import com.study.clothes.service.IGoodsService;
import com.study.clothes.utils.ImgUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.PrintCellComments;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RequestMapping("/goods")
@RestController
public class GoodsController {
    @Autowired
    private IGoodsService iGoodsService;
    @GetMapping("/")
    public List<Goods> goodsList(){

        return iGoodsService.list();

    }
    @PostMapping("/")
    public RespBean add(Goods goods){
        if (iGoodsService.save(goods)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");

    }
    @GetMapping("/getCar")
    public List<Goods> getCar(Integer id){
        return iGoodsService.getCars(id);

    }
    @GetMapping("/page")
    public Map page(int page,int size){
        Map<String, Object> map = iGoodsService.page(page, size);
        return map;
    }
    @PutMapping("/")
    public RespBean update( @RequestParam Goods goods){
        if (iGoodsService.updateById(goods)) {
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteGoods(@PathVariable Integer id){
        if (iGoodsService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");

    }
    @PostMapping("/addAll")
    public RespBean adds(String keyword,Integer page) throws IOException {
        List<Goods> goodsList = ImgUtils.getImg(keyword, page);
        if (iGoodsService.saveBatch(goodsList)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }
    @GetMapping("/getCollection")
    public List<Goods> getCollection(){
        System.out.println();
        return iGoodsService.getCollection();
    }
    @GetMapping("/asc")
    public List<Goods> getGoods(){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("price");

        return iGoodsService.list(queryWrapper);
    }
    @GetMapping("/desc")
    public List<Goods> getGoodsByDesc(){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("price");

        return iGoodsService.list(queryWrapper);
    }
    @GetMapping("/fw")
    public List<Goods> getGoodsF(Integer start,Integer end){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if (start!=null&&end!=null){
            queryWrapper.between("price", start, end);

        }
        if (start==null){
            queryWrapper.lt("price", end);
        }
        if (end==null){
            queryWrapper.gt("price", start);
        }


        return iGoodsService.list(queryWrapper);
    }
    @GetMapping("/getById")
    public Goods getGood(Integer id){
        return iGoodsService.getById(id);
    }
    @GetMapping("/mh")
    public List<Goods> get(String keyword){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(keyword), "NAME", keyword);
       return iGoodsService.list(queryWrapper);
    }
    @GetMapping("/getOrder")
    public List<Goods> getOrder(Integer id,HttpServletRequest request){


        return iGoodsService.getOrder(id);

    }
}
